/*
 * StorageManager.h
 *
 *  Created on: Sep 25, 2011
 *      Author: Daniel
 */

#ifndef STORAGEMANAGER_H_
#define STORAGEMANAGER_H_

#include "Student.h"
#include "Page.h"

class StorageManager {

public:
	static const int NUMBER_OF_PAGES = 10;//2007040;
	//1024 bytes per page, 8 bits per byte, 8192 bits per page = map slots per spacemap page
	//245 spacemap pages == 2007040 pages
	//256 bytes per page == 2001000 pages, 978 spacemap pages

	StorageManager();
	virtual ~StorageManager();

	Page* AllocatePage();
	bool DeallocatePage(Page &pg);
	void PrintSpaceMap(int numRecordsToPrint, ostream &out); //-1 == all

	//Hard-coded convenience functions for project
	void PrintStudent(Page &p);
	Student *FetchStudent(Page &p);

private:
	typedef struct record_structure
	{
		int num_pages;
		int page_size; //size in bytes
		int num_bits_per_int;
		int num_spacemap_pages;
	} record_structure_t;
	typedef record_structure_t* record_structure_ptr;

	void InitializeSpaceMap();
	void ClearPage(Page *pg);
	int GetFreePageNumber();
	void MarkPage(int pageNum, bool used);
	bool IsUsed(int pageNum);

	Page *_bookRoot;
	record_structure_ptr _recordStructure;
	Page *_fileMapPage;
	Page *_firstSpaceMapPage;
	Page *_firstRecordPage;
	int _nextAvailablePage;
};

#endif /* STORAGEMANAGER_H_ */

////Project specific data definitions
//typedef struct student
//{
//	unsigned int SSN;
//	char last_name[32];
//	char first_name[16];
//	char address[64];
//	unsigned short major; //foreign key to another table
//} student_t;
//typedef student_t* student_ptr;

////Storage Manager type definitions
//typedef struct record
//{
//	student_t stdnt; //hardcode record to hold one student entry
//} record_t;

//typedef struct page
//{
//	char page_space[PAGESIZEBYTES];//record_t rcrd; //simplify such that only one record per page
//} page_t;
//typedef page_t* page_ptr;
